package cn.big.bigd.biocode.controller;


import cn.big.bigd.biocode.dao.CategoryDAO;
import cn.big.bigd.biocode.dto.CategoryTree;
import cn.big.bigd.biocode.model.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;
/**
 * @ClassName: CategoryController
 * @Description: TODO
 * @Author: FanWang
 * @CreateDate: 2018/1/24 下午3:06
 * @Version: 1.0
 * @Copyright: Copyright (c) 2018/1/24
 **/

@Controller
@RequestMapping("categories")
public class CategoryController {




    @Autowired
    CategoryDAO categoryDAO;




    @RequestMapping(value = {"", "/"})
    public String home(Model model) {
        List<Category> categories = categoryDAO.findByParentId(null);
        List<CategoryTree> categoryTrees = new ArrayList<>();
        for (Category category : categories) {
            CategoryTree ct = new CategoryTree();
            ct.setRoot(category);
            ct.setChilds(categoryDAO.findByParentId(category.getId()));
            categoryTrees.add(ct);
            model.addAttribute("categoryTrees", categoryTrees);
        }
        return "category/index";
    }

    @RequestMapping(value = "{id}")
    public String id(Model model, @PathVariable Integer id) {
        System.out.println(id);
        List<Category> categories = categoryDAO.findByParentId(id);
        List<CategoryTree> categoryTrees = new ArrayList<>();
        for (Category category : categories) {
            CategoryTree ct = new CategoryTree();
            ct.setRoot(category);
            ct.setChilds(categoryDAO.findByParentId(category.getId()));
            categoryTrees.add(ct);
            model.addAttribute("categoryTrees", categoryTrees);
        }
        return "category/index";
    }



}
